﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ReporterApplication
{
    public partial class ModelForm : Form
    {
        public ModelForm()
        {
            InitializeComponent();
        }

        private void ModelForm_Load(object sender, EventArgs e)
        {
            XmlCIF cif = XmlCIF.Load(Path.Combine(CifFolder, "cif.xml"));

            cif.PrepareMetadataModel();

            //render tree

            foreach (var type in cif.pTypes.OrderBy(x=>x.Label))
            {
                TreeNode tn = new TreeNode(type.Label + "(" + type.setLabel + ")(" + type.Count + ")")
                {
                };
                tn.Tag = type;

                modeltreeView.Nodes.Add(tn);

                var attributes = new TreeNode("Attributes");

                foreach (var attrib in type.Attributes)
                {
                    var attribNode = new TreeNode(attrib.Name + " : " + attrib.Count);
                    attribNode.Tag = attrib;

                    attributes.Nodes.Add(attribNode);
                }

                var referencies = new TreeNode("Referencies");

                foreach (var attrib in type.Referencies)
                {
                    var attribNode = new TreeNode(attrib.Name + " : " + attrib.Count);
                    attribNode.Tag = attrib;
           
                    referencies.Nodes.Add(attribNode);
                }

                var usedBy = new TreeNode("Used By");

                foreach (var attrib in type.UsedBy)
                {
                    var attribNode = new TreeNode(attrib.Name + " : " + attrib.Count);
                    attribNode.Tag = attrib;

                    usedBy.Nodes.Add(attribNode);
                }

                var instances = new TreeNode("Instances");

                foreach (var ints in type.Elements)
                {
                    var attribNode = new TreeNode(ints.Attribute("name").Value);
                    attribNode.Tag = ints;

                    instances.Nodes.Add(attribNode);

                    foreach (var intsEl in ints.Elements().Where(x=>x.Attribute("refid")!=null))
                    {
                        var name = "#" + intsEl.Name.LocalName;
                        
                        var elemNode = new TreeNode(name);

                        attribNode.Nodes.Add(elemNode);
                    }
                }

                tn.Nodes.Add(attributes);

                tn.Nodes.Add(referencies);

                tn.Nodes.Add(usedBy);

                tn.Nodes.Add(instances);
            }

            XmlCIF.Progress = 0;
        }

        public string CifFolder { get; set; }

        private void copybutton_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(commandtextBox.Text);
        }

        private void modeltreeView_Click(object sender, EventArgs e)
        {
           
        }

        private void modeltreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            var command = modeltreeView.SelectedNode.Tag as ITypeCommand;

            if (command != null)
            {
                commandtextBox.Text = command.Command;
            }
        }
    }
}
